<%@ tag language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ attribute name="page" required="true" type="org.springframework.data.domain.PageImpl"%>

<c:set var="pageIndex" value="${page.getNumber() + 1}"></c:set>
<c:set var="totalPages" value="${page.getTotalPages()}"></c:set>
<c:set var="maxPages" value="5"></c:set>
<c:if test="${paging_tag != true }">
	<c:set var="paging_tag" value="true" scope="request"></c:set>
<script>
	
	var pageSelected = function(index){
		var url = location.href;
		var len = url.length;
		if(url.lastIndexOf('#') == len - 1){
			url = url.substring(0, len - 1);
		}
		var i = url.indexOf('page=');
		if(i == -1){
			if(url.indexOf('?') == -1){
				url += "?page=" + index;
			}else{
				url += "&page=" + index;
			}
		}else{
			var j = url.indexOf('&', i);
			if(j == -1){
				url = url.substring(0, i) + 'page=' + index;
			}else{
				url = url.substring(0, i) + 'page=' + index + url.substring(j);
			}
		}
		location.href = url;
	}

</script>
</c:if>

<c:if test="${totalPages > 0}">
<ul class="pagination" style="margin: 0;">
	<li><a href="#">${page.totalElements} records</a></li>
	<li><a href="javascript:pageSelected(1)">&laquo;</a></li>
	<c:forEach var="index" 
		begin="${pageIndex > maxPages ? pageIndex - maxPages : 1 }" 
		end="${totalPages - pageIndex > maxPages ? pageIndex + maxPages : totalPages}">
		
		<li <c:if test="${index eq pageIndex}">class="active"</c:if>>
			<a href="javascript:pageSelected(${index})">${index}</a>
		</li>
	</c:forEach>
	
	<li><a href="javascript:pageSelected(${totalPages})">&raquo;</a></li>
</ul>
</c:if>